package com.example.day15;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main2Activity extends AppCompatActivity {
    ListView listV;
//    MyAdapter myAdapter;
    MyAdapter2 myAdapter;
    LinearLayout linearLayout;
    List<Map<String,Object>> dataList;
    MyHandler myHandler =new MyHandler(this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        listV=findViewById(R.id.list1);
        linearLayout = this.findViewById(R.id.over);
        dataList=new ArrayList<Map<String,Object>>();
        Message message = Message.obtain();
        message.what=100;
        HashMap<String,String> map =new HashMap<>();
        map.put("stage_id",1+"");
        map.put("limit",20+"");
        map.put("page",1+"");
        new WorkThread(WorkThread.STIRING,myHandler,message,"http://www.qubaobei.com/ios/cf/dish_list.php",map).start();


    }

    private static class MyHandler extends Handler {
        private WeakReference<Context> reference;   //
        public MyHandler(Context context) {
            reference = new WeakReference<>(context);//这里传入activity的上下文
        }
        @Override
        public void handleMessage(Message msg) {
            Main2Activity activity = (Main2Activity) reference.get();
            super.handleMessage(msg);
            switch (msg.what) {      //判断标志位
                case 100:
                    String jsonStr = msg.obj.toString();
                    Log.e("jsonSTR",jsonStr);
                    try {
                        JSONObject json= new JSONObject(jsonStr);
                        JSONArray jsA = json.getJSONArray("data");
                        int length = jsA.length();
                        for(int i =0;i<length;i++){
                            Map<String,Object> map = new HashMap<>();
                            JSONObject jsobj = jsA.getJSONObject(i);
                            map.put("title",jsobj.getString("title"));
                            map.put("food_str",jsobj.getString("food_str"));
                            map.put("pic", null);
                            activity.dataList.add(map);
                            Message message = Message.obtain();
                            message.what=300;
                            message.arg1=i;
                            new WorkThread(WorkThread.MAP,activity.myHandler,message,jsobj.getString("pic"),null).start();
                            Log.e("jsonD",i+"");
                        }
                        activity.myAdapter  = new MyAdapter2(activity,activity.dataList);
                        activity.listV.setAdapter(activity.myAdapter);
//                        activity.missHand();
                        activity.listV.setSelection(1);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                case 300:
                    if(msg.obj!=null){
                        activity.dataList.get(msg.arg1).put("pic", (Bitmap)msg.obj);
                        activity.myAdapter.notifyDataSetChanged();
                    }
                    break;
            }

        }
    }
}
